package com.myweb;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/select_table")
public class TableServlet extends HttpServlet {
    private static final Map<String, String> SEAT_TO_TABLE_MAPPING = new HashMap<>();
    static {
        SEAT_TO_TABLE_MAPPING.put("1","1");
        SEAT_TO_TABLE_MAPPING.put("2","2");
        SEAT_TO_TABLE_MAPPING.put("3","3");
        SEAT_TO_TABLE_MAPPING.put("4","4");
        SEAT_TO_TABLE_MAPPING.put("5","5");
        SEAT_TO_TABLE_MAPPING.put("6","6");
        SEAT_TO_TABLE_MAPPING.put("7","7");
        SEAT_TO_TABLE_MAPPING.put("8","8");
        SEAT_TO_TABLE_MAPPING.put("9","9");
        SEAT_TO_TABLE_MAPPING.put("10","10");
        
        
        
        
        // ... 添加其他座位到桌号的映射
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String select_table = request.getParameter("select_table");
        String restaurant_id = request.getParameter("restaurant_id");
        String table_id = SEAT_TO_TABLE_MAPPING.get(select_table);

        if (table_id == null) {
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.println("{\"error\": \"找不到座位\"}");
            return;
        }

        HttpSession session = request.getSession();
        session.setAttribute("table_id", table_id);
        session.setAttribute("restaurant_id", restaurant_id);
        String qrCodeBase64 = generateQRCodeBase64(table_id);

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.println("{\"" + qrCodeBase64 + "\"}");
    }

private String generateQRCodeBase64(String text) {
    // 这里应该调用二维码库来生成图片，并转换为Base64编码
    // 但由于篇幅限制，这里只是返回一个占位符字符串
	
    return "您已选定餐馆和座位";
    
  
    // 模拟的Base64编码
}
}